package boerse.main.Account;

/**
 * Created with IntelliJ IDEA.
 * User: 13102012
 * Date: 09.05.13
 * Time: 23:13
 * To change this template use File | Settings | File Templates.
 */

import boerse.main.Exceptions.NegativeShareCountException;
import boerse.main.Exceptions.NegativeValueException;
import boerse.main.Exceptions.NotEnoughMoneyException;
import boerse.main.Logger.GameLogger;
import boerse.main.StockPriceProvider.StockPriceInfo;

import java.io.IOException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.util.Currency;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Timer;
import java.util.logging.Level;
import java.util.logging.Logger;


public class AccountManagerProxy implements AccountManager {

    protected Logger logger;
    protected AccountManager accountManager;
    private ResourceBundle rb;
    private Locale locale;

    public AccountManagerProxy(StockPriceInfo priceInfo, ResourceBundle rb, Locale locale) throws IOException {
        accountManager = new AccountManagerImpl(priceInfo);
        logger = GameLogger.getLogger("AccountManagerCFG.txt", rb, locale);

        this.rb = rb;
        this.locale = locale;
        logger.log(Level.FINE, rb.getString("initializingAccountManager"));
    }

    @Override
    public void createPlayer(String name) {
        accountManager.createPlayer(name);
        logger.log(Level.INFO, rb.getString("createPlayerAccountManager") + name);
    }

    @Override
    public void buyShare(String playerName, String shareName, int count) throws NotEnoughMoneyException, NegativeValueException {
        accountManager.buyShare(playerName, shareName, count);
        logger.log(Level.INFO, rb.getString("accountManager") + " " + rb.getString("player") + " " + playerName + " " + rb.getString("bought") + " " + count + " " + rb.getString("sharesOf") + " " + shareName);
    }

    @Override
    public void sellShare(String playerName, String shareName, int count) throws NegativeValueException, NegativeShareCountException {
        accountManager.sellShare(playerName, shareName, count);
        logger.log(Level.INFO, "<AccountManager> Player " + playerName + " sold " + count + " shares of " + shareName);
    }

    @Override
    public long getCashAccountPrice(String playerName) {
        long price = accountManager.getCashAccountPrice(playerName);
        logger.log(Level.FINE, "<AccountManager> returns CashAccountPrice of Player " + playerName + " with the value of: " + price);
        return price;
    }

    @Override
    public long getShareDepositPrice(String playerName) {
        long price = accountManager.getShareDepositPrice(playerName);
        logger.log(Level.FINE, "<AccountManager> returns ShareDepositPrice of Player " + playerName + " with the value of: " + price);
        return price;
    }

    @Override
    public long getAllAssets(String playerName) {
        long price = accountManager.getAllAssets(playerName);
        logger.log(Level.FINE, "<AccountManager> returns the Price of All Assets on Player " + playerName + "'s Account with the value of: " + price);
        return price;
    }


    @Override
    public void addMoney(String playername, int money) throws NegativeValueException {
    	DecimalFormat formatter = (DecimalFormat) DecimalFormat.getNumberInstance(locale);
    	formatter.setCurrency(Currency.getInstance(locale));
    	formatter.applyPattern("\u00A4");	//currency sign
        logger.log(Level.INFO, rb.getString("accountManager") + " " + rb.getString("added") + " " + formatter.format(money) + " " + rb.getString("toCashAccountOfPlayer") + " " + playername);
        accountManager.addMoney(playername, money);
    }

    @Override
    public void useAgent(String name) {
        logger.log(Level.INFO, "<AccountManager> Player " + name + " is from now on using an Agent to handle hes trades");
        accountManager.useAgent(name);
    }

    @Override
    public String getShareDeposit(String name) {
        logger.log(Level.FINE, "<AccountManager> is returning the hole ShareDeposit of Player " + name);
        return accountManager.getShareDeposit(name);
    }

	@Override
	public String getTransfers(String playerName, String outputModeString) {
		return accountManager.getTransfers(playerName, outputModeString);
	}

	@Override
	public String getTransfers(String playerName, String sortMode, String outputModeString) {
		return accountManager.getTransfers(playerName, sortMode, outputModeString);
	}


	@Override
	 public String getTransfers(String playerName, String sortMode, String shareName, String outputModeString) {
		return accountManager.getTransfers(playerName, sortMode, shareName, outputModeString);
	}
	
	@Override
	public String getAllShare() {
		return accountManager.getAllShare();
	}
	
	@Override
	public String getAllPlayer() {
		return accountManager.getAllPlayer();
	}
	
	@Override
	public Timer getTimer() {
		return accountManager.getTimer();
	}
}
